home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / spoc88 / dcg / grammar.pro < prev    next >
Text File  |  1988-06-17  |  3KB  |  109 lines

  1. /* Simple DCG parser
  2.    Barbara Clinger, 1988
  3.    
  4.    This program illustrates the expansion of a simple DCG. 
  5.    Its vocabulary consists of:
  6.      Nouns: John, Mary, man, dog;
  7.      Determiners: the, a
  8.      verbs: likes, sees
  9.     
  10.    Sample input: The man sees a dog.
  11.    Output: True or False, for success or failure of parsing.
  12. */
  13.  
  14. domains
  15.     toklist = string*
  16.  
  17. predicates
  18.     reader(string,toklist)            /* the reader */
  19.     remove_period(toklist,toklist)
  20.     append(toklist,toklist,toklist)
  21.     do
  22.    /* The grammar */
  23.     sentence(toklist,toklist,toklist)    /* the parser */
  24.     noun_phrase(toklist)
  25.     verb_phrase(toklist)
  26.     determiner(string)
  27.     noun(string)
  28.     verb(string)
  29. goal
  30.     do.
  31. clauses
  32.  
  33. /* The clause do parses a sentence and returns true or false. Its 
  34.    writing is informational only. */
  35.    
  36. do :-
  37.     nl,write("Enter a sentence --> "),
  38.     readln(S),nl,nl,
  39.     reader(S,List),            /* use the reader */
  40.     write("Output of the reader: ",List),nl,nl,
  41.     remove_period(List, List_in),
  42.     sentence(List_in,Noun_phrase,Verb_phrase),
  43.     write(" Noun phrase: ",Noun_phrase),nl,
  44.     write("Verb phrase: ",Verb_phrase),nl.
  45.  
  46. /*
  47.    Using append to split the List_in into possible noun phrases and
  48.    verb phrases is not efficient, but for simple grammars it is
  49.    adequate.
  50. */
  51.  
  52. /* expansion of:
  53.     sentence --> noun_phrase, verb_phrase
  54. */
  55. sentence(List_in,Noun_list_out,Verb_list_out) :-
  56.     append(Noun_list_out,Verb_list_out,List_in),
  57.     noun_phrase(Noun_list_out),!,verb_phrase(Verb_list_out).
  58.  
  59. /* expansion of:
  60.     noun_phrase --> determiner, noun
  61.     noun_phrase --> noun
  62. */
  63. noun_phrase([A,B]) :- determiner(A),noun(B).
  64. noun_phrase([A]) :- noun(A).
  65.  
  66. /* expansion of:
  67.     verb_phrase --> verb, noun_phrase
  68.     verb_phrase --> verb, noun
  69.     verb_phrase --> verb
  70. */
  71. verb_phrase([A|B]) :- verb(A), noun_phrase(B).    
  72. verb_phrase([A,B]) :- verb(A),noun(B).
  73. verb_phrase([A]) :- verb(A).
  74.  
  75. /* the dictionary */
  76. determiner("the").
  77. determiner("a"). 
  78.  
  79. noun("man"). 
  80. noun("john").
  81. noun("mary").
  82. noun("dog").
  83.  
  84. verb("likes").
  85. verb("sees").
  86. /* end of dictionary */
  87.  
  88. /* reader
  89.    (1) the empty string returns the empty list,
  90.    (2) if the string is not empty, it recursively takes the front 
  91.        token, converts it to lower case, then reads the rest of the 
  92.        list, until the string is empty.
  93. */
  94. reader("",[]) :- !.        
  95. reader(Str,[Token|Rest]) :-
  96.     fronttoken(Str,Tok,Str1),   
  97.     upper_lower(Tok,Token),
  98.     reader(Str1,Rest),!.
  99.  
  100. /* removes the period from list of tokens, if it exists */
  101. remove_period(L1,L2) :-
  102.     append(L2,["."],L1).
  103. remove_period(L1,L1).
  104.  
  105. append([],List,List).
  106. append([H|T],L,[H|T2]) :-
  107.     append(T,L,T2).
  108.         
  109.